package whf.framework.util;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;

/**
 * 计算代码行数
 * @author wanghaifeng
 * @create Sep 10, 2006 8:51:20 PM
 * 
 */
public class LineCounter {
	private static Log log = LogFactory.getLog(LineCounter.class);
	
	/**
	 * 根目录路径
	 * @property String:rootPath
	 */
	private String rootPath;
	
	/**
	 * 文件扩展名
	 * @property String[]:exts
	 */
	private String[] exts;
	
	/**
	 * 文件数量
	 * @property int:fileCount
	 */
	private int fileCount;
	
	/**
	 * 字符数量
	 * @property int:charCount
	 */
	private int charCount;
	
	public LineCounter(String rootPath, String[] exts){
		this.rootPath = rootPath;
		this.exts = exts;
	}
	
	public int count() throws Exception{
		return this.count(new File(this.rootPath), this.exts);
	}
	
	/**
	 * @modify wanghaifeng Oct 16, 2006 3:00:16 PM
	 * @param file
	 * @param exts
	 * @return
	 * @throws Exception
	 */
	public int count(File file, String[] exts) throws Exception{
		if(file.isFile()){
			String fn = file.getAbsolutePath();
			if(exts != null){
				boolean match = false;
				for(int  i=0; i< exts.length; i++){
					if(fn.toLowerCase().endsWith("."+exts[i].toLowerCase())){
						match = true;
						break;
					}
				}
				if(!match){
					return 0;
				}
			}
			int lineCount = 0;
			int charCount = 0;
			LineNumberReader reader = new LineNumberReader(new FileReader(file));
			try{
				String line = reader.readLine();
				while(line != null){
					lineCount ++;
					charCount += line.length();
					line = reader.readLine();
				}
			}catch(Exception e){
				lineCount ++;
			}
			fileCount ++;
			this.charCount += charCount;
			fn = fn.substring(this.rootPath.length());
			log.info("File<"+this.fileCount+"> :"+fn+"\t"+lineCount+":"+charCount);
			return lineCount;
		} else {
			File[] subFiles = file.listFiles();
			int count = 0;
			for(File f: subFiles){
				count += count(f, exts);
			}
			return count;
		}
	}
	
	public static void main(String[] args) throws Exception{
		byte[] buff = new byte[1024];
		int rc = 0;
		String rootPath = "";
		String[] exts = null;
		while(true){
			System.out.print("请输入操作类型：default为统计当前项目的代码行数，exit退出统计：");
			rc = System.in.read(buff);
			String op = new String(buff, 0, rc-2);
			if(StringUtils.equals(op, "exit")){
				break;
			} else if(StringUtils.equals("default", op)){
				rootPath = "E:\\CVSRoot\\framework\\workspace2\\framework\\JavaSource";
				exts = new String[]{"java","xml", "properties"};
			} else{
				System.out.print("请输入根路径：");
				rc = System.in.read(buff);
				rootPath = new String(buff, 0, rc-2);
				System.out.println();
				System.out.print("请输入需要统计的文件的扩展名：");
				buff = new byte[1024];
				rc = System.in.read(buff);
				String extNames = new String(buff, 0, rc-2);
				exts = StringUtils.split(extNames, ",");
			}
			LineCounter lc = new LineCounter(rootPath, exts);
			int totalCount = lc.count();
			log.info("Total Line Count\t"+totalCount);
			log.info("Total Char Count\t"+lc.charCount);
		}
		log.info("End");
	}
}
